package com.wang.jmonkey.common.handler.file;

import com.obs.services.ObsClient;
import com.obs.services.model.PutObjectResult;
import com.wang.jmonkey.common.constant.FileConstant;
import com.wang.jmonkey.common.property.FileSystemProperty;
import com.wang.jmonkey.common.property.HuaWeiSystemProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.io.InputStream;

@Slf4j
public class HuaWeiSystemHandler implements FileSystemHandlerAdapter {

    @Autowired
    private FileSystemProperty fileSystemProperty;

    @Override
    public boolean uploadFile(String filePath, InputStream file) {
        HuaWeiSystemProperty property = fileSystemProperty.getHuaweiObs();

        try (ObsClient obsClient = new ObsClient(property.getAccessKey(), property.getSecretKey(), property.getEndPoint())) {
            PutObjectResult result = obsClient.putObject(property.getBucketName(), filePath , file);

            if (result.getStatusCode() == HttpStatus.SC_OK) {
                return true;
            }
        } catch (IOException e) {
            log.error("HuaWeiSystemHandler uploadFile error : ", e);
        }

        return false;
    }

    @Override
    public String getResourcePrefix() {
        return fileSystemProperty.getHuaweiObs().getResourcePrefix();
    }

    @Override
    public boolean support(String type) {
        return FileConstant.FileSystemType.HUAWEI.getKey().equalsIgnoreCase(type);
    }
}
